package com.hc.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Calendar;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hc.comm.CommData;
import com.hc.service.ExportReportService;
import com.hc.service.GetDataService;
import com.hc.serviceImpl.ExportReportServiceImpl;
import com.hc.serviceImpl.GetDataServiceImpl;
import com.hc.util.DateUtil;
import com.hc.util.DateUtilExt;
import com.hc.util.DownFileUtil;

public class ExportReportServlet  extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
	
		
		
		
//		HttpSession session = req.getSession();  
//		if(session.getAttribute("nameNumber")==null||session.getAttribute("nameNumber").equals("")){
//			return;
//		}
		
		
		String types = req.getParameter("types");  
		
		
		
		//先加载文件夹位置
		if(CommData.sysPath==null||"".equals(CommData.sysPath)){
			
			CommData.sysPath=getServletContext().getRealPath("/");
			
		}
		
		if("rawData".equals(types)){
			
			rawData(req,resp);
			
			
		}else if("gradeData".equals(types)){
			
			grade(req,resp);
		}else if("vlanGrade".equals(types)){
			vlanGrade(req,resp);
			
		}else if("downFile".equals(types)){
			
			
			
			if(req.getParameter("fileName")!=null&&!"".equals(req.getParameter("fileName"))){
				
				DownFileUtil.downFile(req.getParameter("fileName"), resp);
			}
			
			
		}else if("allDataToExcel".equals(types)){
			allDataToExcel(req,resp);
			
		}
		
		
	}
	private void allDataToExcel(HttpServletRequest req, HttpServletResponse resp) {
		String startDate = null;
		String stopDate = null;
	

		String dateType = req.getParameter("dateType");  
		
		
		String date =req.getParameter("date");

		
		if(dateType==null||date==null){
			
			return;
		}
		
		Calendar calendar = DateUtil.str2Calendar(date+" 00:00:00", DateUtil.DEFAULT_STRING);
		
		if("按日".equals(dateType)){
			
			
			 startDate = DateUtil.date2Str(DateUtilExt.getFirstTimeOfDay(calendar.getTime()), DateUtil.DEFAULT_STRING);  
			 stopDate = DateUtil.date2Str(DateUtilExt.getLastTimeOfDay(calendar.getTime()), DateUtil.DEFAULT_STRING); 
			
			
		}else if("按周".equals(dateType)){
			
			 startDate = DateUtil.date2Str(DateUtilExt.getFirstDayOfWeek(calendar.getTime()), DateUtil.DEFAULT_STRING);  
			 stopDate = DateUtil.date2Str(DateUtilExt.getLastDayOfWeek(calendar.getTime()), DateUtil.DEFAULT_STRING); 
			
			
		}else if("按月".equals(dateType)){
			
			 startDate = DateUtil.date2Str(DateUtilExt.getFirstDayOfMoth(calendar.getTime()), DateUtil.DEFAULT_STRING);  
			 stopDate = DateUtil.date2Str(DateUtilExt.getLastDayOfMoth(calendar.getTime()), DateUtil.DEFAULT_STRING); 
		}
			
	
		
		ExportReportService service = new ExportReportServiceImpl();
		String msg = service.getAllData(startDate, stopDate);
		returnMsg(req, resp, msg);
		
		
		}

/**
 * Vlan评分
 * @param req
 * @param resp
 */
	private void vlanGrade(HttpServletRequest req, HttpServletResponse resp) {
		String startDate = null;
		String stopDate = null;
	

		String vlanGradeDateType = req.getParameter("vlanGradeDateType");  
		
		
		String vlanGradeDate =req.getParameter("vlanGradeDate");
		String vlanGradeCompanyText = req.getParameter("vlanGradeCompanyText");  
		String vlanGradeCompanyTextSelect =req.getParameter("vlanGradeCompanyTextSelect");
		
		if(vlanGradeDateType==null||vlanGradeDate==null){
			
			return;
		}
		
		Calendar calendar = DateUtil.str2Calendar(req.getParameter("vlanGradeDate")+" 00:00:00", DateUtil.DEFAULT_STRING);
		
		if("按日".equals(vlanGradeDateType)){
			
			
			 startDate = DateUtil.date2Str(DateUtilExt.getFirstTimeOfDay(calendar.getTime()), DateUtil.DEFAULT_STRING);  
			 stopDate = DateUtil.date2Str(DateUtilExt.getLastTimeOfDay(calendar.getTime()), DateUtil.DEFAULT_STRING); 
			
			
		}else if("按周".equals(vlanGradeDateType)){
			
			 startDate = DateUtil.date2Str(DateUtilExt.getFirstDayOfWeek(calendar.getTime()), DateUtil.DEFAULT_STRING);  
			 stopDate = DateUtil.date2Str(DateUtilExt.getLastDayOfWeek(calendar.getTime()), DateUtil.DEFAULT_STRING); 
			
			
		}else if("按月".equals(vlanGradeDateType)){
			
			 startDate = DateUtil.date2Str(DateUtilExt.getFirstDayOfMoth(calendar.getTime()), DateUtil.DEFAULT_STRING);  
			 stopDate = DateUtil.date2Str(DateUtilExt.getLastDayOfMoth(calendar.getTime()), DateUtil.DEFAULT_STRING); 
		}
		
		
		ExportReportService service = new ExportReportServiceImpl();
		String msg = service.getVlanGradeData(startDate, stopDate,vlanGradeCompanyTextSelect,vlanGradeCompanyText);
		
		returnMsg(req, resp, msg);
	}
/**
 * 指标评分
 * @param req
 * @param resp
 */
	private void grade(HttpServletRequest req, HttpServletResponse resp) {
		String startDate = null;
		String stopDate = null;
	

		String vlanGradeDateType = req.getParameter("gradeDateType");  
		
		
		String vlanGradeDate =req.getParameter("gradeDate");

		
		if(vlanGradeDateType==null||vlanGradeDate==null){
			
			return;
		}
		
		Calendar calendar = DateUtil.str2Calendar(vlanGradeDate+" 00:00:00", DateUtil.DEFAULT_STRING);
		
		if("按日".equals(vlanGradeDateType)){
			
			
			 startDate = DateUtil.date2Str(DateUtilExt.getFirstTimeOfDay(calendar.getTime()), DateUtil.DEFAULT_STRING);  
			 stopDate = DateUtil.date2Str(DateUtilExt.getLastTimeOfDay(calendar.getTime()), DateUtil.DEFAULT_STRING); 
			
			
		}else if("按周".equals(vlanGradeDateType)){
			
			 startDate = DateUtil.date2Str(DateUtilExt.getFirstDayOfWeek(calendar.getTime()), DateUtil.DEFAULT_STRING);  
			 stopDate = DateUtil.date2Str(DateUtilExt.getLastDayOfWeek(calendar.getTime()), DateUtil.DEFAULT_STRING); 
			
			
		}else if("按月".equals(vlanGradeDateType)){
			
			 startDate = DateUtil.date2Str(DateUtilExt.getFirstDayOfMoth(calendar.getTime()), DateUtil.DEFAULT_STRING);  
			 stopDate = DateUtil.date2Str(DateUtilExt.getLastDayOfMoth(calendar.getTime()), DateUtil.DEFAULT_STRING); 
		}
		
		
		ExportReportService service = new ExportReportServiceImpl();
		String msg = service.getGradeData(startDate, stopDate);
		returnMsg(req, resp, msg);
		
		
	}

	private void returnMsg(HttpServletRequest req, HttpServletResponse resp,String msg) {
		
		resp.setCharacterEncoding("utf-8");
//		resp.setHeader("Content-type", "text/html;charset=UTF-8");
		PrintWriter w;
		try {
			w = resp.getWriter();
			w.write(msg);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	/**
	 * 获取原始数据
	 * @param req
	 * @param resp
	 */
	private void rawData(HttpServletRequest req, HttpServletResponse resp) {
		
		Integer pageNumber = Integer.parseInt(req.getParameter("pageNumber"));
		String selectRawTestType = req.getParameter("selectRawTestType");  
		String companySelect =  req.getParameter("companySelect");  
		String companyText =  req.getParameter("companyText");  
		if(req.getParameter("rawDataStartDate")==null||req.getParameter("rawDataStopDate")==null){
			
			return ;
		}
		Calendar StartCalendar = DateUtil.str2Calendar(req.getParameter("rawDataStartDate")+" 00:00:00", DateUtil.DEFAULT_STRING);
		Calendar stopCalendar = DateUtil.str2Calendar(req.getParameter("rawDataStopDate")+" 00:00:00", DateUtil.DEFAULT_STRING);
		String startDate = DateUtil.date2Str(DateUtilExt.getFirstTimeOfDay(StartCalendar.getTime()), DateUtil.DEFAULT_STRING);  
		String stopDate = DateUtil.date2Str(DateUtilExt.getLastTimeOfDay(stopCalendar.getTime()), DateUtil.DEFAULT_STRING); 
		ExportReportService service = new ExportReportServiceImpl();
		

		String msg=service.getRawData(selectRawTestType, companySelect, startDate , stopDate,pageNumber,companyText);
		
		returnMsg(req, resp, msg);
		
	}
}